package com.qianguo.shop.assistant;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.eva.androidlibrary.api.entity.ImageBean;
import com.eva.androidlibrary.widget.mdedittext.MaterialEditText;
import com.qianguo.shop.R;
import com.eva.androidlibrary.api.HttpRequestCallback;
import com.qianguo.shop.base.BaseChooseImageActivity;
import com.eva.androidlibrary.api.entity.ResultData;
import com.qianguo.shop.constant.ShopKeys;
import com.qianguo.shop.entity.KeyValuePair;
import com.qianguo.shop.entity.ShopAssistant;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;

import java.io.File;

/**
 * 编辑店员信息页面
 *
 * @author GUXIA318
 * @date 2015-11-25 15:44
 */
@EActivity(R.layout.activity_shop_assistant_add)
public class AddOrEditShopAssistantActivity extends BaseChooseImageActivity {

    private static final String KEY_SHOP_ASSISTANT = "key_shop_assistant";

    private static final int REQUEST_CODE_POSITION = 0x10;

    /** 店员头像 */
    @ViewById(R.id.shop_assistant_add_avatar_image_view)
    ImageView mImgAvatar;

    /** 店员手机号 */
    @ViewById(R.id.shop_assistant_add_mobile_edit_text)
    MaterialEditText mEdtMobile;

    /** 店员姓名 */
    @ViewById(R.id.shop_assistant_add_name_edit_text)
    MaterialEditText mEdtName;

    /** 性别选择器 */
    @ViewById(R.id.shop_assistant_add_sex_radio_group)
    RadioGroup mRadioGroupSex;

    /** 男 */
    @ViewById(R.id.shop_assistant_add_sex_male_radio_button)
    RadioButton mRadioButtonMale;

    /** 女 */
    @ViewById(R.id.shop_assistant_add_sex_female_radio_button)
    RadioButton mRadioButtonFemale;

    /** 职位Layout */
    @ViewById(R.id.shop_assistant_add_position_layout)
    LinearLayout mPositionLayout;

    /** 职位 */
    @ViewById(R.id.shop_assistant_add_position_tv)
    TextView mTvPosition;

    /** 店员信息 */
    @Extra(KEY_SHOP_ASSISTANT)
    ShopAssistant mShopAssistant;

    /** 店铺ID */
    @Extra(ShopKeys.EXTRA_KEY_SHOP_ID)
    String mShopID;

    private RadioButton mCheckedSex;
    private String mAvatarFilePath;

    private KeyValuePair mPosition;

    public static void jumpToEditShopAssistant(Context context, String shopID, ShopAssistant shopAssistant) {
        Intent intent = new Intent();
        intent.setClass(context, com.qianguo.shop.assistant.AddOrEditShopAssistantActivity_.class);
        intent.putExtra(ShopKeys.EXTRA_KEY_SHOP_ID, shopID);
        intent.putExtra(KEY_SHOP_ASSISTANT, shopAssistant);
        context.startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CODE_POSITION:
                    mPosition = data.getParcelableExtra(ShopKeys.RESULT_KEY_POSITION);
                    mTvPosition.setText(mPosition.getValue());
                    break;
            }
        }
    }

    @Override
    protected String getToolbarTitle() {
        return getString(R.string.shop_assistant_add_title);
    }

    @Override
    protected void initializeUI() {
        mCheckedSex = mRadioButtonMale;
        if (mShopAssistant != null) {
            mEdtName.setText(mShopAssistant.getRealname());
            mEdtMobile.setText(mShopAssistant.getPhone());
            mImageLoader.loadImage(this, mImgAvatar, mShopAssistant.getAvatar());
            String sex = mShopAssistant.getSex();
            if (TextUtils.isEmpty(sex) || sex.equals(mRadioButtonMale.getText().toString())) {
                mRadioButtonMale.setChecked(true);
                mCheckedSex = mRadioButtonMale;
            } else {
                mRadioButtonFemale.setChecked(true);
                mCheckedSex = mRadioButtonFemale;
            }
        }

        mRadioGroupSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.shop_assistant_add_sex_male_radio_button) {
                    mCheckedSex = mRadioButtonMale;
                } else {
                    mCheckedSex = mRadioButtonFemale;
                }
            }
        });
    }

    @Click(R.id.shop_assistant_add_save_button)
    void clickSave() {
        String mobile = mEdtMobile.getText().toString();
        String name = mEdtName.getText().toString();
        String sex = mCheckedSex.getText().toString();

        if (invalidateInfo(mobile, name, mAvatarFilePath, mPosition)) {
            if (TextUtils.isEmpty(mAvatarFilePath)) {
                addToServer(name, mobile, sex, mAvatarFilePath, mPosition.getKey());
            } else {
                uploadAvatar(name, mobile, sex, mAvatarFilePath, mPosition.getKey());
            }
        }
    }

    private boolean invalidateInfo(String mobile, String name, String avatarFilePath, KeyValuePair position) {
        if (TextUtils.isEmpty(mobile)) {
            mEdtMobile.setError(getString(R.string.shop_assistant_add_hint_mobile));
            return false;
        }

        if (TextUtils.isEmpty(name)) {
            mEdtName.setError(getString(R.string.shop_assistant_add_hint_name));
            return false;
        }

        if (position == null) {
            showSnackBar(this, getString(R.string.shop_assistant_add_hint_position));
            return false;
        }

        return true;
    }

    @Background
    void uploadAvatar(final String name, final String mobile, final String sex, final String avatarFilePath, final String positionId) {
        mApp.getApi().uploadImg(mApp.getUser().getToken(), new File(avatarFilePath), new HttpRequestCallback<ImageBean>() {
            @Override
            public void onSuccess(int code, String msg, ImageBean data) {
                addToServer(name, mobile, sex, data.getUrl(), positionId);
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(AddOrEditShopAssistantActivity.this, msg);
            }
        });
    }

    @Background
    void addToServer(String name, String mobile, String sex, String avatarUrl, String positionId) {
        mApp.getApi().addShopAssistant(mApp.getUser().getToken(), mShopID, name, mobile, sex, avatarUrl, positionId, new HttpRequestCallback<ResultData>() {
            @Override
            public void onSuccess(int code, String msg, ResultData data) {

            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(AddOrEditShopAssistantActivity.this, msg);
            }
        });
    }

    @Click(R.id.shop_assistant_add_avatar_image_view)
    void clickAvatar() {
        showPickerDialog(new PicPickerCallback() {
            @Override
            public void afterPick(String filePath) {
                mAvatarFilePath = filePath;
                mImageLoader.loadImage(AddOrEditShopAssistantActivity.this, mImgAvatar, filePath);
            }
        });
    }

    @Background
    void uploadAvatar(String filePath) {
        mApp.getApi().uploadImg(mApp.getUser().getToken(), new File(filePath), new HttpRequestCallback<ImageBean>() {
            @Override
            public void onSuccess(int code, String msg, ImageBean data) {

            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });

    }

    @Click(R.id.shop_assistant_add_position_layout)
    void clickChoosePosition() {
        ShopPositionListActivity.jumpToPositions(this, REQUEST_CODE_POSITION, mShopID);
    }
}
